package com.wfm.leetcode.editor.cn;

import com.wfm.common.TreeNode;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class P剑指OfferII049从根节点到叶节点的路径数字之和{
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public int sumNumbers(TreeNode root) {
        int r = 0;
        dfs(root);
        for (int i = 0; i < res.size(); i++) {
            List<Integer> integers = res.get(i);
            double tmp=0;
//            for (int j = integers.size()-1; j >=0; j--) {
//                tmp=tmp+integers.get(j)*Math.pow(10,integers.size()-j-1);
//            }
            for (int j = 0; j < integers.size(); j++) {
                tmp=tmp*10+integers.get(j);
            }
            r+=tmp;
        }
        return r;
    }
    void dfs(TreeNode root){
        if(root==null){
            return;
        }
        path.add(root.val);
        if(root.left==null&&root.right==null){
            res.add(new ArrayList<>(path));
        }
        dfs(root.left);
        dfs(root.right);
        path.remove(path.size()-1);
    }
}
//leetcode submit region end(Prohibit modification and deletion)

    public static void main(String[] args) { 
        Solution solution = new P剑指OfferII049从根节点到叶节点的路径数字之和().new Solution();
        int i = solution.sumNumbers(TreeNode.generatorTreeNode("1,2,3"));
        System.out.println(i);
    }
}